package com.ttc.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Jiarui on 2016/8/4.
 */
public class common {

    public static String getSalt(){
        String a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String string = "";
        for (int i = 0; i < 9; i++)
        {
            int rand = (int) (Math.random() * a.length());
            string += a.charAt(rand);
        }
        return string;
    }

    public static String getSerialNum(){
        int a = (int) (Math.random() * 10000000);
        String b = String.format("%07d",a);
        return new Date().getTime()+b;
    }

    public static boolean checkEmail(String email){
        boolean flag = false;
        try{
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }

    public static boolean checkMobileNumber(String mobileNumber){
        boolean flag = false;
        try{
            Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }

    public static Boolean isstring(String str){

        Boolean bl = false;

        //首先,使用Pattern解释要使用的正则表达式，其中^表是字符串的开始，$表示字符串的结尾。
        Pattern pt = Pattern.compile("^[0-9a-zA-Z_]+$");

        //然后使用Matcher来对比目标字符串与上面解释得结果
        Matcher mt = pt.matcher(str);

        //如果能够匹配则返回true。实际上还有一种方法mt.find()，某些时候，可能不是比对单一的一个字符串，
        //可能是一组，那如果只要求其中一个字符串符合要求就可以用find方法了.
        if(mt.matches()){

            bl = true;
        }

        return bl;

    }

    public static String SHA1(String decript) {
        try {
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        //ipAddress = this.getRequest().getRemoteAddr();
        ipAddress = request.getHeader("x-forwarded-for");
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if(ipAddress.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress= inet.getHostAddress();
            }

        }

        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
            if(ipAddress.indexOf(",")>0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

    public static Map InterceptForType(){
        Map map = new HashMap();
        Map company = new HashMap();
        Map customer = new HashMap();
        company.put("/home/job/create.action","1");
        company.put("/home/job/insertJob.action","1");
        company.put("/home/job/changeJobType.action","2");
        company.put("/home/job/changeCity.action","2");
        company.put("/home/job/index.action","1");
        company.put("/home/job/detail.action","1");
        company.put("/home/job/getJobList.action","2");
        company.put("/home/job/deleteJobs.action","2");
        company.put("/home/job/edit.action","1");
        company.put("/home/job/save.action","1");
        company.put("/home/job/manager.action","1");
        company.put("/home/job/preview.action","1");
        company.put("/home/resume/getResumeManageList.action","2");
        company.put("/home/resume/resumeManage.action","1");
        company.put("/home/resume/rManageDetail.action","1");
        company.put("/home/resume/updateStatus.action","2");
        company.put("/home/company/changePsd.action","1");
        company.put("/home/company/savePsd.action","2");
        company.put("/home/company/info_view.action","1");
        company.put("/home/company/info_edit.action","1");
        company.put("/home/company/save_company.action","2");
        company.put("/home/company/save_company_detail.action","2");
        company.put("/home/resume/search.action","1");
        company.put("/home/resume/searchResume.action","1");
        company.put("/home/resume/getSearchList.action","2");
        company.put("/home/resume/detail.action","1");
        company.put("/home/resume/getJobList.action","2");
        company.put("/home/resume/sendJob.action","2");
        company.put("/home/resume/detailForCompany.action","1");
        company.put("/home/tag/index.action","1");
        company.put("/home/tag/getTagList.action","2");
        company.put("/home/tag/delete.action","2");
        company.put("/home/tag/insert.action","2");
        company.put("/home/tag/update.action","2");
        company.put("/home/company/mail_box.action","1");
        company.put("/home/company/getMailList.action","2");
        company.put("/home/company/getMailList1.action","2");
        company.put("/home/company/dealDelivery.action","2");
        company.put("/home/customer/read.action","2");
        company.put("/home/company/readInvite.action","2");

        map.put("1",company);
        customer.put("/home/customer/personal.action","1");
        customer.put("/home/customer/edit.action","1");
        customer.put("/home/customer/saveCustomer.action","2");
        customer.put("/home/customer/changePsd.action","1");
        customer.put("/home/customer/savePsd.action","2");
        customer.put("/home/customer/checkExistCustomerPhone.action","2");
        customer.put("/home/customer/checkExistCustomerEmail.action","2");
        customer.put("/home/customer/notice.action","1");
        customer.put("/home/customer/getNoticeList.action","2");
        customer.put("/home/customer/jobDetail.action","1");
        customer.put("/home/customer/jobDetailForSend.action","1");
        customer.put("/home/resume/index.action","1");
        customer.put("/home/resume/getResumeList.action","2");
        customer.put("/home/resume/create.action","1");
        customer.put("/home/resume/getCityList.action","2");
        customer.put("/home/resume/getJobTypeList.action","2");
        customer.put("/home/resume/insertResume.action","2");
        customer.put("/home/resume/insertWorkExp.action","2");
        customer.put("/home/resume/insertComExp.action","2");
        customer.put("/home/resume/insertEdu.action","2");
        customer.put("/home/resume/insertSkill.action","2");
        customer.put("/home/resume/detail.action","1");
        customer.put("/home/resume/deleteResumes.action","2");
        customer.put("/home/resume/edit.action","1");
        customer.put("/home/resume/saveResume.action","2");
        customer.put("/home/resume/saveWorkExp.action","2");
        customer.put("/home/resume/saveComExp.action","2");
        customer.put("/home/resume/saveEdu.action","2");
        customer.put("/home/resume/saveSkill.action","2");
        customer.put("/home/job/search.action","1");
        customer.put("/home/job/searchJob.action","1");
        customer.put("/home/job/getResumeList.action","2");
        customer.put("/home/job/getSearchList.action","2");
        customer.put("/home/job/sendResume.action","2");
        customer.put("/home/job/detail.action","1");
        customer.put("/home/company/info_view.action","1");
        customer.put("/home/customer/mail_box.action","1");
        customer.put("/home/customer/getMailList.action","2");
        customer.put("/home/customer/getMailList1.action","2");
        customer.put("/home/customer/read.action","2");
        customer.put("/home/customer/acceptOrRefuse.action","2");
        map.put("0",customer);
        return map;
    }

}
